<template>
  <div class='container'>
    <el-dialog
      :visible="visibles"
      :title="titleobj"
      @close="closeguan"
      @open="OpeningList"
    >
      <el-form
        label-width="80px"
        :model="formdata"
        :rules="rules"
        ref="formList"
      >
        <el-form-item
          label="所属学科"
          prop="subjectID"
        >
          <el-select
            id="selectt"
            v-model="formdata.subjectID"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in discipline"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"
            ></el-option>

          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="directoryName"
        >
          <el-input
            placeholder="请输入目录名称"
            type="text"
            v-model="formdata.directoryName"
          ></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="closeguan">取 消</el-button>
        <el-button
          type="primary"
          @click="addDatedirectory"
        >确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
import { list as getQuestion } from '@/api/hmmm/subjects'
import { add, detail, update } from '@/api/hmmm/directorys'

export default {
  props: {
    visibles: {
      type: Boolean,
      default: true
    },
    endid: {
      type: Object
    }
  },
  data () {
    return {
      formdata: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      // 学科下拉信息
      discipline: []
    }
  },
  computed: {
    titleobj () {
      return this.formdata.id ? '修改目录' : '新增目录'
    }
  },
  methods: {
    // 关闭弹窗
    closeguan () {
      this.$emit('update:visibles', false)
      this.$refs.formList.resetFields()
      this.formdata = {
        addDate: '',
        creatorId: '',
        id: '',
        state: ''
      }
    },
    // 打开弹窗
    async OpeningList () {
      const { data: res } = await getQuestion()

      this.discipline = res.items
    },
    // 点击添加目录
    addDatedirectory () {
      this.$refs.formList.validate(async (falg) => {
        if (!falg) return
        // if (this.formdata.id === '') {
        //   await add(this.formdata)
        //   this.$message.success('添加成功')
        // } else {
        //   this.formdata.state = 1
        //   await update(this.formdata)
        //   this.$message.success('修改成功')
        // }
        if (this.formdata.id) {
          await update(this.formdata)
          this.$message.success('修改成功')
        } else {
          await add(this.formdata)
          this.$message.success('添加成功')
        }

        // 数据更新
        this.$emit('updatalist')
        // 关闭弹窗
        this.closeguan()
      })
    },
    // 封装修改
    async Modifydata () {
      const { data } = await detail(this.endid)
      this.formdata = data
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog {
  width: 380px;
}
#selectt {
  width: 260px;
  background-color: blueviolet;
}
::v-deep .el-input--suffix {
  width: 260px;
}

.el-input {
  height: 32px;
  line-height: 32px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
